import {
  arriveUpload,
  orderRepairProcess,
  orderSetOff,
} from '@/api/order'
import { useToast } from 'wot-design-uni'

export function getItemStatusDetail(item) {
  const {
    status,
    assignedTime,
    startTime,
    arriveTime,
    processingTime,
    completedTime,
  } = item
  if (status === 'ASSIGNED') {
    item.status_label = '待处理'
    item.status_label_color = '#3b82f6'
    item.status_label_bg = '#dbeafe'
    item.status_time_label = '预约时间'
    item.status_time = assignedTime
    item.status_btn_text = '出发'
    item.status_btn_type = 'primary'
    item.status_icon = 'icon-start'
    item.status_btn_visible = true
  }
  else if (status === 'START') {
    item.status_label = '出发途中'
    item.status_label_color = '#ef4444'
    item.status_label_bg = '#fee2e2'
    item.status_time_label = '出发时间'
    item.status_time = startTime
    item.status_btn_text = '签到'
    item.status_btn_type = 'warning'
    item.status_icon = 'icon-locate'
    item.status_btn_visible = true
  }
  else if (status === 'ARRIVED') {
    item.status_label = '已到达'
    item.status_label_color = '#a855f7'
    item.status_label_bg = '#f3e8ff'
    item.status_time_label = '到达时间'
    item.status_time = arriveTime
    item.status_btn_text = '开始维修'
    item.status_btn_type = 'primary'
    item.status_icon = 'icon-tool'
    item.status_btn_visible = true
  }
  else if (status === 'REPAIRING') {
    item.status_label = '处理中'
    item.status_label_color = '#f97316'
    item.status_label_bg = '#ffedd5'
    item.status_time_label = '开始时间'
    item.status_time = processingTime
    item.status_btn_text = '完成维修'
    item.status_btn_type = 'success'
    item.status_icon = 'icon-finish'
    item.status_btn_visible = true
  }
  else if (status === 'COMPLETED') {
    item.status_label = '已完成'
    item.status_label_color = '#22c55e'
    item.status_label_bg = '#dcfce7'
    item.status_time_label = '完成时间'
    item.status_time = completedTime
    item.status_btn_text = '查看评价'
    item.status_btn_type = 'info'
    item.status_icon = 'icon-star'
    item.status_btn_visible = false
  }
  else {
    item.status_label = ''
    item.status_label_color = ''
    item.status_label_bg = ''
    item.status_time_label = ''
    item.status_time = ''
    item.status_btn_text = ''
    item.status_btn_type = ''
    item.status_icon = ''
    item.status_btn_visible = false
  }
}
export function useGetOrderStatusActions({successFn}: {successFn: Function}) {
  const toast = useToast()
  async function handleSetOff(item) {
    const { orderId } = item
    const res = await orderSetOff({ orderId })
    if (res.code === 0) {
      toast.success('操作成功！')
      successFn()
    } else {
      toast.error({
        msg: '服务器错误，请稍后再试',
      })
    }
  }
  function handleSignIn(item) {
    const { orderId } = item
    uni.getLocation({
      // type: 'wgs84',
      type: 'gcj02',
      success: async (res) => {
        // const latitude = 31.506938
        // const longitude = 121.932293
        const { longitude, latitude } = res
        const res2 = await arriveUpload({ orderId, longitude, latitude })
        if (res2.code === 0) {
          toast.success('签到成功')
          successFn()
        } else {
          toast.error('签到失败，请稍后再试')
        }
      },
      fail() {
        toast.error('获取位置失败，请通过右上角打开定位权限')
      },
    })
  }
  async function handleStartRepair({ orderId }) {
    const res = await orderRepairProcess({ orderId })
    if (res.code === 0) {
      toast.success('维修开始')
      successFn()
    } else {
      toast.error('维修状态上报失败，请稍后再试')
    }
  }
  
  return {
    handleSetOff,
    handleSignIn,
    handleStartRepair
  }
}
